﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web;
using System.Security.Permissions;
using System.ComponentModel;
using System.Web.UI;
using System.Drawing;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;

namespace GandA.Web.UI.Controls
{
    [
    AspNetHostingPermission(SecurityAction.Demand,
        Level = AspNetHostingPermissionLevel.Minimal),
    AspNetHostingPermission(SecurityAction.InheritanceDemand,
        Level = AspNetHostingPermissionLevel.Minimal),
    ]
    [DefaultProperty("Text")]
    [ToolboxData("<{0}:TextBoxNumeric runat=server></{0}:TextBoxNumeric>")]
    [ToolboxBitmap(typeof(System.Web.UI.WebControls.TextBox))]
    public class TextBoxNumeric : System.Web.UI.WebControls.TextBox
    {
        #region Propiedades

        [Bindable(true)]
        [Category("Validator")]
        [DefaultValue("")]
        [Localizable(true)]
        public string ErrorMessage
        {
            get
            {
                return ViewState["_ErrorMessage"] == null ? string.Empty : ViewState["_ErrorMessage"].ToString();
            }

            set
            {
                ViewState["_ErrorMessage"] = value;
            }
        }

        [Bindable(true)]
        [Category("Validator")]
        [DefaultValue("")]
        [Localizable(true)]
        public string ErrorMessageExpression
        {
            get
            {
                return ViewState["_ErrorMessageExpression"] == null ? string.Empty : ViewState["_ErrorMessageExpression"].ToString();
            }

            set
            {
                ViewState["_ErrorMessageExpression"] = value;
            }
        }

        [Bindable(true)]
        [Category("Validator")]
        [DefaultValue(false)]
        [Localizable(true)]
        public bool EnabledValidator
        {
            get
            {
                return ViewState["_EnabledValidator"] == null ? false : (bool)ViewState["_EnabledValidator"];
            }
            set
            {
                ViewState["_EnabledValidator"] = value;
            }
        }

        [Bindable(true)]
        [Category("Validator")]
        [DefaultValue(false)]
        [Localizable(true)]
        public bool EnabledValidatorExpression
        {
            get
            {
                return ViewState["_EnabledValidatorExpression"] == null ? false : (bool)ViewState["_EnabledValidatorExpression"];
            }
            set
            {
                ViewState["_EnabledValidatorExpression"] = value;
            }
        }

        [Bindable(true)]
        [Category("Validator")]
        [DefaultValue("")]
        [Localizable(true)]
        public string ValidationExpression
        {
            get
            {
                return ViewState["_ValidationExpression"] == null ? string.Empty : ViewState["_ValidationExpression"].ToString();
            }
            set
            {
                ViewState["_ValidationExpression"] = value;
            }
        }

        [Bindable(true)]
        [Category("Default")]
        [DefaultValue(null)]
        [Localizable(true)]
        public decimal? Value
        {
            get
            {
                return ViewState["_Value"] == null ? null : Convert.ToDecimal(ViewState["_ValidationExpression"]) as decimal?;
            }

            set
            {
                ViewState["_Value"] = value;
            }
        }
        #endregion
        
        #region Metodos
        
        protected override void OnPreRender(EventArgs e)
        {
            bool linkIncluded = false;
            foreach (Control c in Page.Header.Controls)
            {
                if (c.ID == "StyleControls")
                {
                    linkIncluded = true;
                }
            }
            if (!linkIncluded)
            {
                HtmlGenericControl csslink = new HtmlGenericControl("link");
                csslink.ID = "StyleControls";
                csslink.Attributes.Add("href", Page.ClientScript.GetWebResourceUrl(this.GetType(), "GandA.Web.UI.Controls.Controls.css"));
                csslink.Attributes.Add("type", "text/css");
                csslink.Attributes.Add("rel", "stylesheet");
                Page.Header.Controls.Add(csslink);
            }

            base.OnPreRender(e);

            string resourceName = "GandA.Web.UI.Controls.TextBoxNumeric.js";
            ClientScriptManager cs;
            if (this.Page == null)
                cs = this.Parent.Page.ClientScript;
            else
                cs = this.Page.ClientScript;
            cs.RegisterClientScriptResource(typeof(GandA.Web.UI.Controls.TextBoxNumeric), resourceName);
        }

        protected override void Render(HtmlTextWriter writer)
        {
            this.Attributes.Add("onchange", "return ValidNumber(this,1);");

            if (this.EnabledValidator)
            {
                this.Attributes.Add("onchange", "ValidNumber(this,1); ValidObject(this);");
            }

            if (this.EnabledValidator)
                writer.AddAttribute("ErrorMessage", this.ErrorMessage);
            
            base.Render(writer);
        }
        #endregion
    }
}
